package com.woniuxy.goods.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import com.woniuxy.goods.entity.ProcessModel;
import com.woniuxy.goods.entity.UpgoodsLog;
import com.woniuxy.goods.form.AddUpGoodsLogForm;
import com.woniuxy.goods.param.AddUpGoodsLogParam;
import com.woniuxy.goods.service.UpgoodsLogService;
import com.woniuxy.web.util.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * 典当物品日志管理控制层接口
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@Api(tags = "典当物品日志管理接口")
@RequestMapping("/upgoodsLog")
public class UpgoodsLogController {
    /**
     * 典当物品日志管理服务层接口
     */
    @Resource
    private UpgoodsLogService upgoodsLogService;

    /**
     * 新增典当物品日志记录
     * @param form 新增物品日志对象
     * @return 物品日志对象
     * @throws Exception
     */
    @ApiOperation("新增典当物品日志记录")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = UpgoodsLog.class)
    })
    @PostMapping("/addUpGoodsLog")

    public Result addUpGoodsLog(@Validated @RequestBody AddUpGoodsLogForm form, BindingResult br) throws Exception{
        if(br.hasErrors())throw  new ParamException(10001,"参数有误");
        UpgoodsLog log = upgoodsLogService.addUpGoodsLog(BeanUtil.toBean(form, AddUpGoodsLogParam.class));
        return Result.success("新增典当物品日志记录成功",log);
    }

    /**
     * 批量新增典当物品日志记录
     * @param forms 新增物品日志对象集合
     * @return 物品日志对象集合
     * @throws Exception
     */
    @ApiOperation("批量新增典当物品日志记录")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = UpgoodsLog.class)
    })
    @PostMapping("/addUpGoodsLogList")
    public Result addUpGoodsLogList(@RequestBody List<AddUpGoodsLogForm> forms) throws Exception{
        List<AddUpGoodsLogParam> params = ConvertUtil.convertListTo(forms, AddUpGoodsLogParam::new);
        List<UpgoodsLog> logs = upgoodsLogService.addUpGoodsLogList(params);
        return Result.success("批量新增典当物品日志记录成功",logs);
    }

    /**
     * 通过物品id典当物品日志记录
     * @param goodsId 通过物品id
     * @return 物品日志对象集合
     * @throws Exception
     */
    @ApiOperation("通过物品id典当物品日志记录")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = UpgoodsLog.class)
    })
    @GetMapping("/findUpGoodsLog")
    public Result findUpGoodsLog(Integer goodsId) throws Exception{
        List<UpgoodsLog> logs = upgoodsLogService.findUpGoodsLog(goodsId);
        return Result.success("通过物品id典当物品日志记录成功",logs);
    }

}

